package com.jr.controller;

import com.jr.entry.BookInfo;
import com.jr.entry.Users;
import com.jr.service.UsersService;


import com.jr.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UsersController {
    private int i=120;
    @Autowired
    private UsersService usersService;

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    /*@RequestMapping("login")
    public ModelAndView login(String user_code, String password, HttpSession session){
        ModelAndView mv=new ModelAndView();
        Users user=new Users();
        user.setUser_code(user_code);
        user.setPassword(password);
        Users loginUser = usersService.login(user);
        List<BookInfo> book = usersService.selectAllBook();
        //mv.addObject("user",loginUser);
        session.setAttribute("user",loginUser);
        //session.setAttribute("book",book);
        //mv.addObject("book",book);
        mv.setViewName("show");
        return mv;
    }*/

    @RequestMapping("login")
    @ResponseBody
    public boolean log(String user_code,String password){
        Users user=new Users();
        user.setUser_code(user_code);
        user.setPassword(password);
        return usersService.login(user);
    }


    @RequestMapping(value = "/getAll", produces = "application/json;charset=utf-8")
    @ResponseBody
    public PageBean<BookInfo> findAll(Integer currentPage, Integer pageSize){
        //System.out.println(currentPage+";"+pageSize);
        return usersService.selectAllBook(currentPage,pageSize);
    }

    @RequestMapping(value = "/find", produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<BookInfo> find(Integer book_type,String book_name,Integer is_borrow){

        if (book_type==-1){
            book_type=null;
        }
        if (book_name==""){
            book_name=null;
        }
        if (is_borrow==-1){
            is_borrow=null;
        }
        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_type(book_type);
        bookInfo.setBook_name(book_name);
        bookInfo.setIs_borrow(is_borrow);
        return usersService.selectBookByInfo(bookInfo);
    }

    @RequestMapping(value = "/findByCode",produces = "application/json;charset=utf-8")
    @ResponseBody
    public BookInfo findByBookCode(String book_code){
        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_code(book_code);
        return usersService.selectBookByBookCode(bookInfo);
    }

    @RequestMapping( "update")
    @ResponseBody
    public boolean updateBook(String book_code,Integer book_type,String book_name,String book_author,String publish_press,Integer is_borrow){
        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_code(book_code);
        bookInfo.setBook_type(book_type);
        bookInfo.setBook_name(book_name);
        bookInfo.setBook_author(book_author);
        bookInfo.setPublish_press(publish_press);
        bookInfo.setIs_borrow(is_borrow);
        return usersService.updateBook(bookInfo);
    }

    @RequestMapping("delete")
    @ResponseBody
    public boolean deleteBook(String book_code){
        return usersService.deleteBook(book_code);
    }

    @RequestMapping("add")
    @ResponseBody
    public boolean addBook(String book_code,String book_name,Integer book_type,String book_author,String publish_press){
        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_code(book_code);
        bookInfo.setBook_name(book_name);
        bookInfo.setBook_type(book_type);
        bookInfo.setBook_author(book_author);
        bookInfo.setPublish_press(publish_press);
        bookInfo.setIs_borrow(0);
        return usersService.addBook(bookInfo);
    }

}
